package duty_info

import (
	"fmt"
	"github.com/bitly/go-simplejson"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"net/http"
	"tao-admin/configs"
	"tao-admin/internal/model"
)

type DutyView struct {
	DutyService *DutyService
}

func NewDutyView() *DutyView {
	return &DutyView{
		DutyService: NewDutyService(),
	}

}

func (view *DutyView) searchDutyInfoView(ctx *gin.Context) {
	resp := ""

	//获取参数
	if project, isExist := ctx.GetQuery("project"); isExist == true {
		onDutyInfos, _, err := view.DutyService.GetDutyInfos(ctx, project)
		if err != nil {
			resp = fmt.Sprintf("error:%v", err)
		}
		for _, v := range onDutyInfos {
			resp = resp + fmt.Sprintf(v.Duty) + ","
		}
		//resp = fmt.Sprintf("onDutyInfos:%v", onDutyInfos)
	} else {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"retcode": -1,
			"data":    nil,
			"message": "必传参数：project！！！！！",
		})
		return
	}

	ctx.JSON(200, gin.H{
		"retcode": 0,
		"msg":     "success",
		"data":    resp,
	})

}

func createDutyInfoView(ctx *gin.Context) {
	db := configs.GetGormDB()
	rData, _ := ioutil.ReadAll(ctx.Request.Body)
	sParamJson, err := simplejson.NewJson(rData)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"retcode": -1,
			"data":    nil,
			"message": "请检查入参是否有误， error:" + err.Error(),
		})
	}

	entry := model.OnDutyTab{}
	project, err1 := sParamJson.Get("project").String()
	type1, err2 := sParamJson.Get("type").String()
	key1, _ := sParamJson.Get("key").String()
	value1, err4 := sParamJson.Get("value").String()
	duty, _ := sParamJson.Get("duty").String()
	stime, err6 := sParamJson.Get("stime").Int64()
	etime, err7 := sParamJson.Get("etime").Int64()
	component, err8 := sParamJson.Get("component").String()

	if err8 != nil {
		component = ""
	}
	if err1 != nil || err2 != nil || err4 != nil || err6 != nil || err7 != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"retcode": -1,
			"data":    nil,
			"message": "必船参数：project、type、value、stime、etime",
		})
		return
	} else {
		entry.Project = project
		entry.Type = type1
		entry.Key = key1
		entry.Value = value1
		entry.Duty = duty
		entry.Component = component
		entry.Stime = stime
		entry.Etime = etime
	}

	//向DB中插入数据
	if err := db.Create(&entry).Error; err != nil {
		ctx.JSON(200, gin.H{
			"retcode": -1,
			"data":    nil,
			"message": err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"retcode": 0,
		"data":    entry,
		"message": "新增成功！！！！！",
	})
}

func searchDutyInfoView(ctx *gin.Context) {
	ondutyInfos := make([]model.OnDutyTab, 0)
	db := configs.GetGormDB()
	total := 0

	//获取参数
	if project, isExist := ctx.GetQuery("project"); isExist == true {
		db = db.Where("project=?", project)
	} else {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"retcode": -1,
			"data":    nil,
			"message": "必传参数：project！！！！！",
		})
		return
	}

	if key1, isExist := ctx.GetQuery("key"); isExist == true {
		db = db.Where("key=?", key1)
	}
	if value1, isExist := ctx.GetQuery("value"); isExist == true {
		db = db.Where("value=?", value1)
	}
	if duty, isExist := ctx.GetQuery("duty"); isExist == true {
		db = db.Where("duty=?", duty)
	}
	if component, isExist := ctx.GetQuery("component"); isExist == true {
		db = db.Where("component=?", component)
	}

	err1 := db.Model(&model.OnDutyTab{}).Count(&total).Error
	db = db.Order("project DESC, type ASC")

	err2 := db.Find(&ondutyInfos).Error
	if err1 != nil || err2 != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"total":   total,
			"retcode": -1,
			"data":    nil,
			"message": "查询出错，请检查DB数据或者请求数据是否正确！！！！！",
		})
		return
	}

	ctx.JSON(200, gin.H{
		"retcode": 0,
		"msg":     "success",
		"data":    ondutyInfos,
	})
}
func deleteDutyInfoView(ctx *gin.Context) {
	ctx.JSON(200, gin.H{
		"retcode": 0,
		"msg":     "success",
		"data":    "deleteDutyInfo！！！！！",
	})
}

func updateDutyInfoView(ctx *gin.Context) {
	ctx.JSON(200, gin.H{
		"retcode": 0,
		"msg":     "success",
		"data":    "updateDutyInfo！！！！！",
	})
}
